<?
ob_start();
session_start();
include("../conf/config.php");
include("../conf/database.php");
include("../conf/tools.php");

$curTime=getIndianDateTime();
$prg_id=$_GET["prg_id"];
$mode=$_GET["mode"]; 

foreach($_POST as $key => $value) { $arr_data[$key]=$value;}
foreach($arr_data as $key => $value) { $arr_data[$key]=addslashes($value); }

$arr_data["rec_status"]=1;

$target_path = "../data/prg_brochures/";


$actual_path=$target_path . $prg_id . "/";
if(!is_dir($target_path)) mkdir($target_path);
if(!is_dir($actual_path)) mkdir($actual_path);

$target_path_other = $actual_path."/others/";
if(!is_dir($target_path_other)) mkdir($target_path_other);


if($prg_id>0){

update_qry("bkitc_programs",$arr_data,"prg_id=".$prg_id);
qry_data("update bkitc_programs set last_updated='".$curTime."' where prg_id=".$prg_id);


}
else{

	$prg_id=insert_qry("bkitc_programs",$arr_data);
	qry_data("update bkitc_programs set prg_created='".$curTime."' where prg_id=".$prg_id);

}



$target_path = $target_path . basename( $_FILES['prg_brochure']['name']); 
if(move_uploaded_file($_FILES['prg_brochure']['tmp_name'],$actual_path.$_FILES['prg_brochure']['name'])) {
	$arr_data["prg_brochure"]=$_FILES['prg_brochure']['name'];
	update_qry("bkitc_programs",$arr_data,"prg_id=".$prg_id);
} 


$attachmentInfo=@array_pop(qry_data("select prg_attachments from bkitc_programs where prg_id=".$prg_id));


if($attachmentInfo["prg_attachments"]!=""){
$arr_files=getPrgAttachmentInfo($attachmentInfo["prg_attachments"]);
}
else{

	for($j=0;$j<6;$j++){
	
		$fileInfo=Array("title"=>"", "filename"=>"");
		$arr_files[]=$fileInfo;
	}
}

for($j=0;$j<6;$j++){
$fileCtr=$j+1;
$target_path_actual=$target_path_other.basename( $_FILES['file'. $fileCtr]['name']); 

	//$fileInfo=Array("title"=>"", "filename"=>"");
	//$arr_files[]=$fileInfo;
	
	if(move_uploaded_file($_FILES['file'.$fileCtr]['tmp_name'],$target_path_actual)) {
	
		$fileInfo=Array("title"=>$arr_data["title".$fileCtr], "filename"=>basename( $_FILES['file'.$fileCtr]['name']));
		$arr_files[$j]=$fileInfo;
	} 
	
	if($arr_data["title".$fileCtr]!="") $arr_files[$j]["title"]=$arr_data["title".$fileCtr];

	if($_POST["chkFileRemove".$j]==1){
	$fileInfo=Array("title"=>"", "filename"=>"");
	$arr_files[$j]=$fileInfo;
	}
}

print_r($_POST["chkFileRemove"]);
$arr_data["prg_attachments"]=formatPrgAttachmentInfo($arr_files);
update_qry("bkitc_programs",$arr_data,"prg_id=".$prg_id);

header("location:edit_prg.php?mode=edit&prg_id=".$prg_id);
?>



